package cn.itsource.test;

import org.I0Itec.zkclient.ZkClient;
import org.junit.Test;

import java.util.List;

public class Zk_test {

    /**
     * new ZkClient("127.0.0.1:2181",5000); 创建zookeeper客户端
     * client.getChildren("/")：获取子节点 “/”代表根节点
     * client.createPersistent：创建持久节点
     * client.createPersistentSequential：创建持久有顺节点，会在path后面增加序号
     * client.createEphemeral：创建临时节点
     * client.createEphemeralSequential：创建临时有序节点
     * client.subscribeChildChanges：订阅子节点的改变
     * client.subscribeDataChanges：订阅某个节点的数据改变
     * @throws Exception
     */

    @Test//持久化节点
    public void test1() throws Exception {
        //创建客户端   超时时间
        ZkClient client = new ZkClient("127.0.0.1:2181",5000);
        //获取根节点
        List<String> children = client.getChildren("/zookeeper");
        for (String child : children) {
            System.out.println(child);  //zookeeper
        }

        //创建持久节点  一直都在
        client.createPersistent("/zookeeper/createPersistent");


        //创建临时节点
//        client.createEphemeral("/zookeeper/createEphemeral");
        //client.createEphemeral("/zookeeper/createEphemeral"); //重复创建会报错





        //创建持久顺序节点
//        String persistentSequential =
//                client.createPersistentSequential("/zookeeper/createPersistentSequential", "111");
//        System.out.println("persistentSequential="+persistentSequential);
//  /zookeeper/createPersistentSequential0000000003

//        client.delete("/zookeeper/createPersistentSequential0000000004");

        //创建临时顺序节点
//        String ephemeralSequential =
//                client.createEphemeralSequential("/zookeeper/createEphemeralSequential", "111");
//        System.out.println(" ephemeralSequential="+ephemeralSequential);



        //关闭   是因为这个close   我们的临时节点  只要连接在就不会自己清除
        client.close();
    }
}
